package com.aqie.easy.string;

import java.util.HashMap;

/**
 * 788 旋转数字
 */
public class RotatedDigits {

    HashMap<Character, Character> map = new HashMap<>();

    /**
     * 0 1 8
     * 2 5 6 9
     * 3 4 7
     * @param N
     * @return
     */
    public int rotatedDigits(int N) {
        map.put('0','0');
        map.put('1','1');
        map.put('8','8');
        map.put('2','5');
        map.put('5','2');
        map.put('6','9');
        map.put('9','6');

        // 1. 判断含有347 false
        // 2. 定义翻转规则
        int ans = 0;
        for (int i = 2; i <= N; i++){
            if (!isValid(i)) continue;
            int rotate = rotate(i);
            if(rotate == i) continue;
            System.out.println(i);
            ans++;
        }
        return ans;
    }

    /**
     * 不包含 347 中任何一个返回true
     * @param num
     * @return
     */
    private boolean isValid(int num){
        String s = String.valueOf(num);
        return !s.contains("3") && !s.contains("4") && !s.contains("7");
    }

    private int rotate(int num){
        // int 转 char array
        char[] s = String.valueOf(num).toCharArray();
        for (int i = 0; i < s.length ;i++){
           s[i] =  map.get(s[i]);
        }
        // char array 转 int
        String res = new String(s);
        return Integer.valueOf(res);
    }

    public static void main(String[] args) {
        RotatedDigits r = new RotatedDigits();

        r.rotatedDigits(12);
    }
}
